
#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    pLog = logmanager::getInstance();
    pTimer = new timer();
    pTimer->start();
    pThreadMount = new threadmount();

    // SLOT AND SIGNAL
    qRegisterMetaType<Monture> ("Monture");

    QObject::connect(pLog, SIGNAL(DisplayLogMessage(QString,int)), this,
            SLOT(DisplayLogMessage(QString,int)), Qt::AutoConnection); //Qt::QueuedConnection);
    QObject::connect(pTimer,SIGNAL(timeSignal(QString)),this,SLOT(DisplayTime(QString)));

    QObject::connect(pThreadMount, SIGNAL(SendStatusSerial(QString,int)), this,
            SLOT(DisplaySerialButton(QString,int)), Qt::AutoConnection); //Qt::QueuedConnection);

    QObject::connect(pThreadMount, SIGNAL(SendInfos(Monture)), this,
            SLOT(DisplayInfoMount(Monture)), Qt::AutoConnection); //Qt::QueuedConnection);

    QObject::connect(ui->pushButton, SIGNAL(clicked()),this, SLOT(QuitApp()));

    if (pThreadMount)
    {
        // Main preference -> Move Nord
        QObject::connect(ui->MontMoveN, SIGNAL(pressed ()), pThreadMount,
            SLOT(MoveN()));
        // Main preference -> STOP sur fin de Move Nord
        QObject::connect(ui->MontMoveN, SIGNAL(released ()), pThreadMount,
                SLOT(Stop()));
        // Main preference -> Move Est
        QObject::connect(ui->MontMoveE, SIGNAL(pressed ()), pThreadMount,
                SLOT(MoveE()));
        // Main preference -> STOP sur fin de Move Nord
        QObject::connect(ui->MontMoveE, SIGNAL(released ()), pThreadMount,
                SLOT(Stop()));
        // Main preference -> Move Ouest
        QObject::connect(ui->MontMoveW, SIGNAL(pressed ()), pThreadMount,
                SLOT(MoveW()));
        // Main preference -> STOP sur fin de Move Nord
        QObject::connect(ui->MontMoveW, SIGNAL(released ()), pThreadMount,
                SLOT(Stop()));
        // Main preference -> Move Sud
        QObject::connect(ui->MontMoveS, SIGNAL(pressed ()), pThreadMount,
                SLOT(MoveS()));
        // Main preference -> STOP sur fin de Move Nord
        QObject::connect(ui->MontMoveS, SIGNAL(released ()), pThreadMount,
                SLOT(Stop()));
        // Main preference -> Bouton stop
        QObject::connect(ui->MontStop, SIGNAL(pressed ()), pThreadMount,
            SLOT(Stop()));

        QObject::connect(pThreadMount, SIGNAL(PeriphError(int,int)), this,
            SLOT(Error(int,int)));
    }


   #ifdef __linux__
       stringOS = "RASPBERRY LINUX";
   #endif
   #ifdef _WIN32
       stringOS = "DELL PC WINDOWS";
   #endif

    this->setWindowTitle("CTRL MOUNT FOR " + stringOS);
    pThreadMount->start();
}

MainWindow::~MainWindow()
{
    delete ui;
}



void MainWindow::DisplayInfoMount(Monture mountInf)
{
    QString label;
    label.sprintf("%d:%d:%d",mountInf.CurrentCoord.DEC_deg,mountInf.CurrentCoord.DEC_minute,mountInf.CurrentCoord.DEC_seconde);
    ui->lcdDEC->display(label);
    label.sprintf("%d:%d:%d",mountInf.CurrentCoord.RA_heure,mountInf.CurrentCoord.RA_minute,mountInf.CurrentCoord.RA_seconde);
    ui->lcdRA->display(label);
}

void MainWindow::DisplayLogMessage(QString line, int /*code*/)
{
        LogNbLine++;
        QTextCursor tc = ui->LogLabel->textCursor();
        if (LogNbLine > 200)
        {
            ui->LogLabel->clear();
            LogNbLine = 0;
        }
        ui->LogLabel->insertHtml(line);
        ui->LogLabel->ensureCursorVisible();
        ui->LogLabel->moveCursor(QTextCursor::End);
        tc.movePosition(QTextCursor::End, QTextCursor::KeepAnchor);
}

void MainWindow::DisplayTime(QString val)
{
    ui->lcdHorloge->display((val));
}

void MainWindow::QuitApp()
{
    INT32 TimeOut = 2000;
    pLog->AddMessage(INFO, 1000 + OK, __LINE__, __FILE__,"Quit application");
    if (pTimer != NULL)
    {
        pTimer->setMode(STOP);

        if (!pTimer->wait(TimeOut))
        {
            // time out
            pLog->AddMessage(WARNING, 1000 + DATAFALSE, __LINE__, __FILE__, "Timer time out");
            pTimer->terminate();
        }
        delete pTimer;
        pTimer = NULL;
    }

    if (pThreadMount != NULL)
    {
        pThreadMount->setMode(STOP);

        if (!pThreadMount->wait(TimeOut))
        {
            // time out
            pLog->AddMessage(WARNING, 1000 + DATAFALSE, __LINE__, __FILE__, "Thread Mount time out");
            pThreadMount->terminate();
        }
        delete pThreadMount;
        pThreadMount = NULL;
    }
    if (pLog)
        pLog->kill();
    qApp->quit();
}

void MainWindow::DisplaySerialButton(QString text, int state)
{

    ui->ButtonStatus->setText(text);
    switch (state)
    {
    case SUCCESS:
        ui->ButtonStatus->setStyleSheet("background-color: green;color: white;");
        break;

    case WARNING:
        break;

    case CRITICAL:
        ui->ButtonStatus->setStyleSheet("background-color: red;color: white;");
        break;

    default:
        break;
    }

}


void MainWindow::Error(int periph, int status)
{
    switch(periph)
    {

        case MONT:
        if (status == SUCCESS)
        {
            DisplaySerialButton("MONT OK",status);

        } else
        {
            DisplaySerialButton("MONT ERROR",status);
        }
        break;

    default:
        break;



    }


}
